//
//  RPAppActivityView.m
//  ShowYork
//
//  Created by hxh on 2021/1/18.
//  Copyright © 2021 hxh. All rights reserved.
//

#import "RPAppActivityView.h"

typedef void(^doneBlock)(void);
typedef void(^cancelBlock)(void);

@interface RPAppActivityView ()
@property (nonatomic,strong)UIImageView *containView;
@property (nonatomic,strong)UIImageView *deleteImgView;
@property (nonatomic,strong)HomePageActivityModel *itemModel;

@property (nonatomic,copy)doneBlock done;
@property (nonatomic,copy)cancelBlock cancel;
@end

@implementation RPAppActivityView


- (instancetype)initWithActivityItemModel:(HomePageActivityModel *)itemModel completeBlock:(void (^)(void))completeBlock cancelBlock:(void (^)(void))cancelBlock{
    if (self = [super init]) {
        self.itemModel = itemModel;
        [self setUI];
        if (completeBlock) {
            self.done = ^{
                completeBlock();
            };
        }
        if (cancelBlock) {
            self.cancel = ^{
                cancelBlock();
            };
        }
    }
    return self;
}

- (void)setUI{
    self.frame = CGRectMake(0, 0, kScreenW, kScreenH);
    self.backgroundColor = RGBA(0, 0, 0, 0.5);
    
    self.containView = [[UIImageView alloc]init];
    self.containView.layer.cornerRadius = 4;
    self.containView.contentMode = UIViewContentModeScaleAspectFill;
    self.containView.layer.masksToBounds = YES;
//    self.containView.backgroundColor = UIColor.clearColor;
    [self addSubview:self.containView];
    
    
    // 判断是否需要显示删除按钮
    // 不可关闭 隐藏按钮
    if (!self.itemModel.close) {
        [self.containView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.left.equalTo(self).offset(42);
            make.right.equalTo(self).offset(-42);
            make.height.mas_equalTo((kScreenW - 42*2)/3*4);
        }];
    }else{
        [self.containView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self).offset(-16);
            make.left.equalTo(self).offset(42);
            make.right.equalTo(self).offset(-42);
            make.height.mas_equalTo((kScreenW - 42*2)/3*4);
        }];
        self.deleteImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"activity_delete_tag"]];
        [self addSubview:self.deleteImgView];
        [self.deleteImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.containView);
            make.top.equalTo(self.containView.mas_bottom).offset(30);
        }];
        self.deleteImgView.hidden = YES;
    }
    [self.containView sd_setImageWithURL:[NSURL URLWithString:self.itemModel.eventPicture]];
    
    UITapGestureRecognizer *coverTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(coverMethod)];
    self.containView.userInteractionEnabled = YES;
    [self.containView addGestureRecognizer:coverTap];
    
    UITapGestureRecognizer *cancelTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelMethod)];
    
    self.deleteImgView.userInteractionEnabled = YES;
    [self.deleteImgView addGestureRecognizer:cancelTap];
    
}
- (void)showInView:(UIView *)view{
    
    
    [view addSubview:self];
//    [[UIApplication sharedApplication].keyWindow addSubview:self];
    //第一步：将view宽高缩至无限小（点）
    self.containView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.3, 0.3);
    [UIView animateWithDuration:0.5 animations:^{
        //第一步，以动画形式将view慢慢放大至原始大的1.2倍
        self.containView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.2, 1.2);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.2 animations:^{
            self.containView.transform = CGAffineTransformIdentity;
            self.deleteImgView.hidden = NO;
        }];
    }];
}

- (void)dismiss{
    [UIView animateWithDuration:0.3 animations:^{
        //第一步：以动画形式将view慢慢放大至原始大小的1.2倍
        self.containView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.3, 0.3);
    } completion:^(BOOL finished) {
        //第三步:移除
        if (finished) {
            [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
            [self removeFromSuperview];
        }
    }];
}
- (void)cancelMethod{
    [self dismiss];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.35 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (self.cancel) {
            self.cancel();
        }
    });

}

- (void)coverMethod{
    [self dismiss];
    if (self.done) {
        self.done();
    }
}

@end
